package com.atguigu.tingshu.album.api;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.tingshu.album.service.TrackInfoService;
import com.atguigu.tingshu.common.login.AuthCheck;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.album.TrackInfo;
import com.atguigu.tingshu.query.album.TrackInfoQuery;
import com.atguigu.tingshu.vo.album.TrackInfoVo;
import com.atguigu.tingshu.vo.album.TrackListVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@Tag(name = "声音管理")
@RestController
@RequestMapping("api/album")
@SuppressWarnings({"all"})
public class TrackInfoApiController {

	@Autowired
	private TrackInfoService trackInfoService;

	/**
	 * 腾讯云点播上传声音
	 * @param file 音频文件
	 * @return
	 */
	@Operation(summary = "阿里云点播上传声音")
	@PostMapping("/trackInfo/uploadTrack")
	public Result<JSONObject> uploadTrack(MultipartFile file) {
		JSONObject jsonObject = trackInfoService.uploadTrack(file);
		return Result.ok(jsonObject);
	}

	/**
	 * 保存声音
	 * @param trackInfoVo 保存声音相关信息
	 * @return
	 */
	@AuthCheck
	@Operation(summary = "保存声音")
	@PostMapping("/trackInfo/saveTrackInfo")
	public Result saveTrackInfo(@RequestBody @Validated TrackInfoVo trackInfoVo) {
		Long userId = AuthContextHolder.getUserId();
		trackInfoService.saveTrackInfo(userId, trackInfoVo);
		return Result.ok();
	}

	/**
	 * 获取当前用户声音分页列表
	 * @param page 当前页
	 * @param limit 每页记录数
	 * @param trackInfoQuery 模糊查询属性
	 * @return
	 */
	@AuthCheck
	@Operation(summary = "获取当前用户声音分页列表")
	@PostMapping("/trackInfo/findUserTrackPage/{page}/{limit}")
	public Result<Page<TrackListVo>> findUserTrackPage(@PathVariable int page, @PathVariable int limit,
													   @RequestBody TrackInfoQuery trackInfoQuery) {
		Long userId = AuthContextHolder.getUserId();
		Page<TrackListVo> pageInfo = new Page<>(page, limit);
		trackInfoQuery.setUserId(userId);
		pageInfo = trackInfoService.findUserTrackPage(pageInfo, trackInfoQuery);
		return Result.ok(pageInfo);
	}

	/**
	 * 查询声音信息
	 * @param id 根据id查询声音信息 -- 修改的回显消息
	 * @return
	 */
	@Operation(summary = "查询声音信息")
	@GetMapping("/trackInfo/getTrackInfo/{id}")
	public Result<TrackInfo> getTrackInfo(@PathVariable Long id) {
		TrackInfo trackInfo = trackInfoService.getById(id);
		return Result.ok(trackInfo);
	}

	/**
	 * 根据声音id修改声音信息
	 * @param id 声音id
	 * @param trackInfoVo 修改的声音信息vo
	 * @return
	 */
	@Operation(summary = "修改声音信息")
	@PutMapping("/trackInfo/updateTrackInfo/{id}")
	public Result updateTrackInfo(@PathVariable Long id, @RequestBody TrackInfoVo trackInfoVo) {
		trackInfoService.updateTrackInfo(id, trackInfoVo);
		return Result.ok();
	}


	/**
	 * 根据声音id删除声音
	 * @param id 声音id
	 * @return
	 */
	@Operation(summary = "删除声音信息")
	@DeleteMapping("/trackInfo/removeTrackInfo/{id}")
	public Result removeTrackInfo(@PathVariable Long id) {
		trackInfoService.removeTrackInfo(id);
		return Result.ok();
	}

}

